import message from './message.js';

let cos = new COS({
	SecretId: 'AKIDwhBPN4UdXCcldEYcPwCBtMXR4PUJr8By',
	SecretKey: 'YopY0BD5h25rMaXDdsrZ0A3ey1nIWT9X',
});
const common = {
	toJson: (object) => {
		return JSON.stringify(object);
	},
	toObject: (valString) => {
		return JSON.parse(valString);
	},
	deepClone: (object) => {
		return common.toObject(common.toJson(object));
	},
	compareObjectDeep: (val1, val2) => {
		return common.toJson(val1) === common.toJson(val2);
	},
	isEmptyString: function(target) {
		return ((target === undefined) || (target === null) || (target.trim() === ""));
	},
	submitFiles: function(files, onCustomProgress) {
		return new Promise(function(success, error) {
			let uploadFiles = [];
			for (let index in files) {
				let fileItem = files[index];
				
				if (fileItem.file === undefined) {
					continue;
				}
				
				let uploadFile = {};
				uploadFile.Bucket = 'temp-1308528568';
				uploadFile.Region = 'ap-beijing';
				uploadFile.key = fileItem.file.name;
				uploadFile.Body = fileItem.file;
				uploadFile.onTaskReady = function(taskId) {
					console.log(taskId);
					fileItem.taskId = taskId;
					uploadFile.taskId = taskId;
				}
				uploadFiles.push(uploadFile);
			}
			
			cos.uploadFiles({
				files: uploadFiles,
				SliceSize: 1024 * 1024 * 10,
				onProgress: function(progressData) {
					if (onCustomProgress) {
						onCustomProgress(progressData);
					}
				},
				onFileFinish: function (err, data, options) {
					console.log(options.Key + '上传' + (err ? '失败' : '完成'));
				}
			}, function(err, data) {
				console.log(err || data);
				success(data);
			});

		});
	},
	submitFile: function(coverFileObject) {

		return new Promise(function(success, error) {

			cos.putObject({
				Bucket: 'temp-1308528568',
				/* 填入您自己的存储桶，必须字段 */
				Region: 'ap-beijing',
				/* 存储桶所在地域，例如ap-beijing，必须字段 */
				Key: coverFileObject.file.name,
				/* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
				StorageClass: 'STANDARD',
				Body: coverFileObject.file, // 上传文件对象
				onProgress: function(progressData) {
					coverFileObject.onProgress(progressData);
				}
			}, function(err, data) {
				if (err) {
					error(err);
				}

				if (data) {
					success(data);
				}
			});
		});

	},
	editorUploadFile: (resultFiles, insertImgFn) => {
		const file = resultFiles[0];
		message.loading().show();
		cos.sliceUploadFile({
			Bucket: 'temp-1308528568', // 存储桶名称
			Region: 'ap-beijing', // 存储桶地域
			Key: file.name, // 文件名称
			Body: resultFiles[0], // 文件
		}, function(err, data) {
			message.loading().hide();
			if (err) {
				message.error("图片上传失败");
				return;
			}

			insertImgFn('//' + data.Location); // 插入返回的url地址
		});
	},

	editorUploadVideo: (resultFiles, insertVideoFn) => {
		const file = resultFiles[0];
		message.loading().show();
		cos.sliceUploadFile({
			Bucket: 'temp-1308528568', // 存储桶名称
			Region: 'ap-beijing', // 存储桶地域
			Key: file.name, // 文件名称
			Body: resultFiles[0], // 文件
		}, function(err, data) {
			message.loading().hide();
			if (err) {
				message.error("视频上传失败");
				return;
			}
			insertVideoFn('//' + data.Location); // 插入返回的url地址
		});
	},
}
export default common;
